﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XiMiModel;
using System.Data.SqlClient;
using Dapper;
using XiMiModelDto;
using Microsoft.Extensions.Configuration;

namespace XiMiDal
{
    public class OrderDal
    {
        private readonly AppDbContext _appDbContext;
        public IConfiguration Configuration { get; }
        public OrderDal(AppDbContext appDbContext, IConfiguration configuration)
        {
            _appDbContext = appDbContext;
            Configuration = configuration;
        }
        public List<OrderDtoModel> ShowOrder()
        {
            using(var db=new SqlConnection(Configuration.GetConnectionString("MSSQL")))
            {
                return db.Query<OrderDtoModel>("select * from [Order]").ToList();
            }
        }
        public int AddOrder(OrderModel orderModel)
        {
            using var db = new SqlConnection(Configuration.GetConnectionString("MSSQL"));
            var result=  db.Execute($"insert into [Order] values ('{orderModel.OId}','{orderModel.ONumber}','{orderModel.UId}','{orderModel.OStatus}','{orderModel.Info}','{orderModel.TotalPrice}','{orderModel.AId}')");
            return result;
        }
        public List<OrderDtoModel> Show(int pageindex, int pagesize, out int totalcount)
        {
            var query = from a in _appDbContext.Orders
                        join b in _appDbContext.Orderss on a.ONumber equals b.ONumber
                        join c in _appDbContext.Goodss on b.GId equals c.GId
                        select new OrderDtoModel
                        {
                            OId = a.OId,
                            ONumber = b.ONumber,
                            TotalPrice=a.TotalPrice,
                            Info = a.Info,
                            OStatus = a.OStatus,
                            GId=c.GId,
                            GName=c.GName,
                            GImg= "http://localhost:20338/StaticFiles/"+c.GImg,
                            RNum=c.GNum
                        };
            totalcount = query.Count();
            return query.OrderBy(m => m.OId).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
        }
        public AddressModel Fan(int id)
        {
            return _appDbContext.Addresss.FirstOrDefault(m => m.AId == id);
        }
        public List<FixDtoModel> Check(int id)
        {
            var query = from b in _appDbContext.Addresss 
                        join e in _appDbContext.Citys on b.PId equals e.PId
                        join c in _appDbContext.Citys on b.CId equals c.PId
                        join d in _appDbContext.Citys on b.TId equals d.PId
                        where b.AId == id
                        select new FixDtoModel
                        {
                            AId = b.AId,
                            AName = b.AName,
                            AddressName = b.AddressName,
                            Phone = b.Phone,
                            Remark = b.Remark,
                            You = b.You,
                            PId = e.PId,
                            PName = e.PName,
                            CId = c.CId,
                            CName = c.PName,
                            TId = d.PId,
                            TName = d.PName
                        };
            return query.ToList();
        }
        public List<OrderShowModel> ShowOrders()
        {
            var query = from a in _appDbContext.Orders
                        join b in _appDbContext.Addresss on a.AId equals b.AId
                        join c in _appDbContext.Users on a.UId equals c.UId
                        select new OrderShowModel
                        {

                            OId = a.OId,
                            ONumber = a.ONumber,
                            OStatus = a.OStatus,
                            AId = a.AId,
                            Info = a.Info,
                            UId = a.UId,
                            TotalPrice = a.TotalPrice,
                            ordersModels = (from x in _appDbContext.Orderss
                                            join y in _appDbContext.Goodss on x.GId equals y.GId
                                            where x.ONumber == a.ONumber
                                            select new OrdersDtoModel {
                                                GId = x.GId,
                                                RId = x.RId,
                                                GName=y.GName,
                                                GImg= "http://localhost:20338/StaticFiles/"+y.GImg,
                                            }).ToList()
                        };
            return query.ToList();
        }
    }
}
